package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.edu.dao.BaseDao;
import com.edu.dao.UserComentsDao;
import com.edu.entity.CommentDTO;
import com.edu.entity.ContentVote;
import com.edu.entity.User;
import com.edu.entity.UserComents;

public class UserComentsDaoImpl implements UserComentsDao{
	private BaseDao bd1 = new BaseDao();
	@Override
	public List<CommentDTO> selectAllUserComents( int sellerId) {
		ArrayList<CommentDTO> uclist = new ArrayList();
		Connection conn = bd1.getConn();
		String sql = "SELECT t1.content,t1.created_at,t1.score,t1.comment_id,t2.username,t2.headimg,t2.userid FROM goodsComments t1 "
				+ "INNER JOIN user t2 ON t1.user_id = t2.userid "
				+ "WHERE t1.target_user_id = ?";

		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, sellerId);
//			System.out.println(ps);
			rs = ps.executeQuery();
			while(rs.next()) {
				CommentDTO commentDTO = new CommentDTO();
				commentDTO.setUserId(rs.getInt("userid"));
				commentDTO.setCommentId(rs.getInt("comment_id"));
				commentDTO.setContent(rs.getString("content"));
				commentDTO.setCreated_at(rs.getDate("created_at"));
				commentDTO.setUsername(rs.getString("username"));
				commentDTO.setHeadimg(rs.getString("headimg"));
				commentDTO.setScore(rs.getDouble("score"));
				uclist.add(commentDTO);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return uclist;
	}
	@Override
	public int selectAllUserComentNums(int target_user_id) {
		Connection conn = bd1.getConn();
		int rows = 0;
		String sql = "SELECT count(1) as tatolNum FROM goodsComments WHERE 1=1 ";
		if(target_user_id!=0) {
			sql +=" AND target_user_id = ?";
		}
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			if(target_user_id!=0) {
				ps.setInt(1, target_user_id);
			}
			rs = ps.executeQuery();
			if(rs.next()) {
				rows = rs.getInt("tatolNum");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return rows;
	}
	@Override
	public CommentDTO selectComentsByUserId(int userId,int commentId) {
		Connection conn = bd1.getConn();
		String sql = "SELECT t1.content, t1.created_at, t2.username, t2.headimg, t3.voteType AS votetype, t1.score FROM goodsComments t1 INNER JOIN user t2 ON t1.user_id = t2.userid LEFT JOIN content_vote t3 ON t1.comment_id = t3.commentId AND t3.userid = ? WHERE t1.comment_id = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, userId);
			ps.setInt(2, commentId);
			System.out.println(ps);
			rs = ps.executeQuery();
			while(rs.next()) {
				CommentDTO commentDTO = new CommentDTO();
				commentDTO.setContent(rs.getString("content"));
				commentDTO.setCreated_at(rs.getDate("created_at"));
				commentDTO.setUsername(rs.getString("username"));
				commentDTO.setHeadimg(rs.getString("headimg"));
				commentDTO.setVoteType(rs.getInt("voteType"));
				commentDTO.setScore(rs.getDouble("score"));
//				System.out.println(commentDTO.toString());
				return commentDTO;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			bd1.closeAll(conn, ps, rs);
		}
		return null;
	}
	@Override
	public int likeOrDislikeNums(int commentId,int voteType) {
		Connection conn = bd1.getConn();
		int rows = 0;
		String sql = "SELECT count(1) as tatolNum FROM content_vote WHERE commentId = ? AND voteType = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, commentId);
			ps.setInt(2, voteType);
			rs = ps.executeQuery();
			if(rs.next()) {
				rows = rs.getInt("tatolNum");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			bd1.closeAll(conn, ps, rs);
		}
		return rows;
	}
	@Override
	public int insertInto(ContentVote contentVote) {
		String sql = "INSERT INTO content_vote(commentId,userid,voteType) VALUES(?,?,?)";
		Object[] obj = {contentVote.getCommentId(),contentVote.getUserid(),contentVote.getVoteType()};
		return bd1.executeUpdate(sql, obj);
	}
	@Override
	public ContentVote seleteContentVote(ContentVote cv) {
		Connection conn = bd1.getConn();
		String sql = "SELECT * FROM content_vote WHERE commentId=? AND userId=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			System.out.println(ps);
			ps.setInt(1, cv.getCommentId());
			ps.setInt(2, cv.getUserid());
			rs = ps.executeQuery();
			while(rs.next()) {
				ContentVote Mycv = new ContentVote();
				Mycv.setCommentId(rs.getInt("commentId"));
				Mycv.setContentVoteId(rs.getInt("contentVoteId"));
				Mycv.setUserid(rs.getInt("userid"));
				Mycv.setVoteType(rs.getInt("voteType"));
//				System.out.println(commentDTO.toString());
				return Mycv;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			bd1.closeAll(conn, ps, rs);
		}
		return null;
	}
	@Override
	public int updateContentVote(ContentVote cv) {
		String sql = "UPDATE content_vote SET voteType=? WHERE contentVoteId = ?";
		Object[] obj = {cv.getVoteType(), cv.getContentVoteId()};
		return bd1.executeUpdate(sql, obj);


	}
	@Override
	public int insetComent(UserComents uc) {
		String sql = "INSERT INTO goodscomments(user_id,target_user_id,content,created_at,score) VALUES(?,?,?,NOW(),?) ";
		Object[] obj = {uc.getUser_id(),uc.getTarget_user_id(),uc.getContent(),uc.getScore()};
		return bd1.executeUpdate(sql, obj);
	}
}
